
package org.apache.osgimaker.analyse.algorithm.dependency;

import org.apache.osgimaker.analyse.algorithm.graph.AtomicVertex;
import org.apache.osgimaker.analyse.algorithm.util.StringPattern;


/**
 * Result of a dependency check.
 * 
 */
public class DependencyResult implements Result
{
  static final String OK = "\tOK";
  static final String DEPENDENCIES_FOUND = "\n  Unexpected dependencies found:";
  private final StringPattern _startSet;
  private final StringPattern _finalSet;
  private final String _statement;
  private final AtomicVertex[] _paths;
  private final boolean _ok;
  
  public DependencyResult(StringPattern startSet, StringPattern finalSet,
                          String statement, AtomicVertex[] paths)
  {
    _startSet = startSet;
    _finalSet = finalSet;
    _statement = statement;
    _paths = paths;
    _ok = paths.length == 0;
  }

  /**
   * Returns <code>true</code> if and only if {@link #getPaths()} is empty.
   */
  public boolean isOk()
  {
    return _ok;
  }

  /**
   * Returns the statement causing this result.
   */
  public String getStatement()
  {
    return _statement;
  }

  /**
   * Returns the pattern describing the final set.
   */
  public StringPattern getFinalSet()
  {
    return _finalSet;
  }

  /**
   * Returns the vertices of the paths of unwanted dependencies.
   * 
   * @return an empty array if no unwanted dependencies have been found.
   */
  public AtomicVertex[] getPaths()
  {
    return _paths;
  }

  /**
   * Returns the pattern describing the start set.
   */
  public StringPattern getStartSet()
  {
    return _startSet;
  }

  public String toString()
  {
    StringBuffer buffer = new StringBuffer(_statement);
    if (_ok)
    {
      buffer.append(OK);
    } else
    {
      DependencyPathsRenderer renderer = new DependencyPathsRenderer(_paths, _startSet, _finalSet);
      buffer.append(DEPENDENCIES_FOUND).append(renderer.renderGraph("  "));
    }
    return new String(buffer.append('\n'));
  }
}
